package ljl.codetop300;

public class _213_rob {

  static class off1 {
    public int rob(int[] nums) {
      int length = nums.length;
      if (length == 1) {
        return nums[0];
      } else if (length == 2) {
        return Math.max(nums[0], nums[1]);
      }
      return Math.max(robRange(nums, 0, length - 2), robRange(nums, 1, length - 1));
    }

    public int robRange(int[] nums, int start, int end) {
      int first = nums[start], second = Math.max(nums[start], nums[start + 1]);
      for (int i = start + 2; i <= end; i++) {
        int temp = second;
        second = Math.max(first + nums[i], second);
        first = temp;
      }
      return second;
    }
  }

  static class copy_off1 {
    public int rob(int[] nums) {
      if (nums.length == 1) return nums[0];
      if (nums.length == 2) return Math.max(nums[0], nums[1]);
      return Math.max(max(nums, 0, nums.length - 2), max(nums, 1, nums.length - 1));
    }
    int max(int[] nums, int from, int to) {
      int a = nums[from];
      int b = Math.max(a, nums[from + 1]);
      for (int i = from + 2; i <= to; i++) {
        int t = b;
        b = Math.max(b, a + nums[i]);
        a = t;
      }
      return b;
    }
  }
}
